from common.email_helper import email_send_with_appendix_list
from common.logger_helper import g_wlogger
from common.pandas_helper import PandasHelper
from k3_data_report.business.cgtj_db import Cgtj
from k3_data_report.business.config import bussiness_cal_fields
from k3_data_report.finance.zhmx_db import Zjzhmx

__author__ = "Ypf"
_date_ = "2019/9/11-16:16"

def zhmx(time1):
    db_sql = bussiness_cal_fields['k3_caozuo']
    check_sql = db_sql["insert_check_sql"] % ('zjzhmx', time1)
    check_dic = Cgtj.check_cf(check_sql)
    if len(check_dic) == 0:
        sql=db_sql['zhmx_sql']
        db_insert_sql=db_sql['zhmx_insert']
        dic=Zjzhmx.get_zjzhmx(sql)
        Zjzhmx.inser_db(db_insert_sql,dic,time1)
    else:
        g_wlogger.winfo(f"zhmx-{time1}:target_full_df, all data is empty.")


def zhmx_main(month):
    try:
        zhmx(month)
        g_wlogger.winfo(f"资金账户明细表月数据{month}统计完成")
        return True
    except Exception as e:
        g_wlogger.werror(f"zhmx_main has error:{e} with month:{month}")
        return False


def create_df_zhmx(time1=None):
        db_sql = bussiness_cal_fields['k3_caozuo']
        sql = db_sql['zhmx_sql']
        dic = Zjzhmx.get_zjzhmx(sql)
        list_out=[]
        for key in dic:
            end=tuple(dic[key])
            list_out.append(end)
        columns = ["账户名称", "账户币种", "初期余额", f"{time1}月收入", f"{time1}月转入", f"{time1}月付款", f"{time1}月转出", "管理费用",
                   "客户费用", "产品费用", "期末库存现金"]
        k3zhmx_df = PandasHelper.create_dataframe(data=list_out, columns=columns)
        return k3zhmx_df



